﻿package com.game.module.gameTask.view
{
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.vo.EquipmentVO;
	import com.game.data.vo.PetDataVO;
	import com.game.manager.NotificationManager;
	import com.game.module.gameTask.vo.TaskVO;
	import com.game.view.layer.LayerBase;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class TaskInterface extends LayerBase
	{
		
		public function TaskInterface()
		{
			super();
		}
		
		public var btn_close:SimpleButton;
		public var dailymc:MovieClip;
		public var activitymc:MovieClip;
		public var getaward:MovieClip;
		public var prepage:SimpleButton;
		public var nextpage:SimpleButton;
		public var txtinstr:TextField;
		public var txtcur:TextField;
		public var t1:TaskTitle;
		public var t2:TaskTitle;
		public var t3:TaskTitle;
		public var t4:TaskTitle;
		public var t5:TaskTitle;
		public var alist1:AwardItem;
		public var alist2:AwardItem;
		public var alist3:AwardItem;
		public var alist4:AwardItem;
		public var txtpage:TextField;
		private var curPage:uint = 1;
		private var allPage:uint = 1;
		private var curAry:Array = [];
		private var selectTask:TaskVO;
		private var selectId:uint = 0;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("TaskInterface") as MovieClip;
			TextUtil.redrawTxt(mc);
			btn_close = mc.getChildByName("btn_close") as SimpleButton;
			prepage = mc.getChildByName("prePage") as SimpleButton;
			nextpage = mc.getChildByName("nextPage") as SimpleButton;
			dailymc = mc.getChildByName("dailymc") as MovieClip;
			dailymc.gotoAndStop(1);
			activitymc = mc.getChildByName("activitymc") as MovieClip;
			activitymc.gotoAndStop(1);
			getaward = mc.getChildByName("getaward") as MovieClip;
			getaward.gotoAndStop(1);
			
			txtinstr = mc.getChildByName("txtinstr") as TextField;
			txtpage = mc.getChildByName("txtpage") as TextField;
			txtcur = mc.getChildByName("txtcur") as TextField;
			
			setCloseBtn(btn_close);
			var tmp:*;
			for (var i:int = 1; i <= 5; i++)
			{
				this["t" + i] = new TaskTitle();
				tmp = mc.getChildByName("t" + i);
				with(this["t" + i] as TaskTitle)
				{
					x = tmp.x;
					y = tmp.y;
				}
				mc.addChildAt(this["t" + i],mc.getChildIndex(tmp));
				mc.removeChild(tmp);
			}
			
			
			for (var j:int = 1; j <= 4; j++)
			{
				this["alist" + j] = new AwardItem();
				tmp = mc.getChildByName("alist" + j);
				with(this["alist" + j] as AwardItem)
				{
					x = tmp.x;
					y = tmp.y;
				}
				mc.addChildAt(this["alist" + j],mc.getChildIndex(tmp));
				mc.removeChild(tmp);
			}
			addChild(mc);
			dailymc.buttonMode = true;
			activitymc.buttonMode = true;
			getaward.buttonMode = true;
		}
		
		public function analyseAward():void
		{
			var _loc1_:int;
			var _loc2_:* = null;
			var _loc3_:* = null;
			var _loc4_:* = 0;
			if (this.selectTask == null)
			{
				return;
			}
			this.taskAwardAryById();
			if (this.selectTask.rwaward == null)
			{
				throw new Error("奖品为null！");
			}
			var _loc5_:* = "恭喜";
			_loc1_ = this.selectTask.rwaward.length;
			var _loc6_:* = [];
			while (_loc1_-- > 0)
			{
				switch (this.selectTask.rwaward[_loc1_].type)
				{
					case "dj":
					case "zzs":
					case "bs":
						_loc2_ = this.gc.allEquip.findByName(this.selectTask.rwaward[_loc1_].value);
						_loc5_ += "您成功领取:" + _loc2_.ename;
						_loc6_.push(_loc2_);
						break;
					case "exp":
						if (this.gc.playerData)
						{
							_loc3_ = this.gc.playerData.findCurrentPet();
							if (_loc3_ != null)
							{
								_loc3_.setCurExper(_loc3_.getCurExper() + int(this.selectTask.rwaward[_loc1_].value));
							}
							else
							{
								gc.playerData.addExp(int(this.selectTask.rwaward[_loc1_].value));
							}
						}
						if (_loc3_)
						{
							_loc5_ += "您宠物成功领取:" + this.selectTask.rwaward[_loc1_].value + " 经验";
						}
						else
						{
							_loc5_ += "您成功领取:" + this.selectTask.rwaward[_loc1_].value + " 经验";
						}
						break;
					case "lh":
						gc.playerData.addLhValue(int(this.selectTask.rwaward[_loc1_].value));
						_loc5_ += "您成功领取:" + this.selectTask.rwaward[_loc1_].value + " 灵魂";
						break;
					case "roomhorse":
						this.gc.playerData.petList.push(this.getroomhorsePet());
						_loc5_ += "您成功领取炎马";
						break;
				}
			}
			_loc4_ = uint(_loc6_.length);
			while (_loc4_-- > 0)
			{
				if (this.gc.playerData.roleId > 0)
				{
					if (EquipmentVO(_loc6_[_loc4_]).type == "zbwp" || EquipmentVO(_loc6_[_loc4_]).type == "wpqhs")
					{
						this.gc.putQhsInBackPack(this.gc.playerData, EquipmentVO(_loc6_[_loc4_]).equipId);
					}
					else
					{
						this.gc.playerData.bagEquipList.push(_loc6_[_loc4_]);
					}
				}
			}
			NotificationManager.alert(_loc5_);
			this.selectTask.setHasGetAward(true);
			this.getaward.removeEventListener(MouseEvent.CLICK, this.awardClick);
			this.getaward.gotoAndStop(1);
			this.setTaskList(this.curAry);
		}
		
		private function setTaskList(param1:Array):void
		{
			var titleIdx:int;
			for (titleIdx = 0; titleIdx < 5; titleIdx++)
			{
				this["t" + (titleIdx + 1)].visible = true;
				this["t" + (titleIdx + 1)].removeEventListener(MouseEvent.CLICK, this.selected);
				(this["t" + (titleIdx + 1)] as TaskTitle).removeIcon();
			}
			for (titleIdx = 0; titleIdx < 5; titleIdx++)
			{
				if (param1[titleIdx + (this.curPage - 1) * 5])
				{
					(this["t" + (titleIdx + 1)] as TaskTitle).addEventListener(MouseEvent.CLICK, this.selected);
					(this["t" + (titleIdx + 1)] as TaskTitle).setTask(param1[titleIdx + (this.curPage - 1) * 5] as TaskVO);
				}
				else
				{
					this["t" + (titleIdx + 1)].visible = false;
				}
			}
			if (this.selectId != 0)
			{
				this["t" + this.selectId].dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
		
		private function setAwardList():void
		{
			var _loc1_:* = null;
			var _loc2_:* = 0;
			var _loc3_:int = 0;
			this.alist1.removeImage();
			this.alist2.removeImage();
			this.alist3.removeImage();
			this.alist4.removeImage();
			if (this.selectTask)
			{
				_loc1_ = this.selectTask.getAllAward();
				_loc2_ = uint(_loc1_.length);
				_loc3_ = 0;
				while (_loc3_ < _loc2_)
				{
					if (_loc1_[_loc3_].type == "exp")
					{
						(this["alist" + (_loc3_ + 1)] as AwardItem).addImage("rw_exp");
					}
					else if (_loc1_[_loc3_].type == "bs")
					{
						(this["alist" + (_loc3_ + 1)] as AwardItem).addImage("rw_bs");
					}
					else if (_loc1_[_loc3_].type == "lh")
					{
						(this["alist" + (_loc3_ + 1)] as AwardItem).addImage("rw_lh");
					}
					else if (_loc1_[_loc3_].type == "roomhorse")
					{
						(this["alist" + (_loc3_ + 1)] as AwardItem).addImage("rw_roomhorse");
					}
					else
					{
						(this["alist" + (_loc3_ + 1)] as AwardItem).addImage(_loc1_[_loc3_].value);
					}
					(this["alist" + (_loc3_ + 1)] as AwardItem).itemName.text = _loc1_[_loc3_].cname;
					_loc3_++;
				}
			}
		}
		
		private function setTaskListToFirstFrame():void
		{
			for (var idx:int = 0; idx < 5; idx++)
			{
				(this["t" + (idx + 1)] as TaskTitle).unselected();
			}
		}
		
		private function setPageTxt():void
		{
			this.txtpage.text = this.curPage + "/" + this.allPage;
		}
		
		private function setTxt():void
		{
			if (this.selectTask)
			{
				this.txtinstr.text = this.selectTask.getrwdict();
				this.txtcur.text = this.selectTask.getTaskPro();
			}
		}
		
		private function taskAwardAryById():void
		{
			var _loc1_:* = null;
			var _loc2_:uint = this.selectTask.allReward.length;
			var _loc3_:uint = Math.round(Math.random() * (_loc2_ - 1));
			if (this.selectTask.allReward[_loc3_])
			{
				_loc1_ = {};
				_loc1_.type = this.selectTask.allReward[_loc3_].type;
				if (this.selectTask.allReward[_loc3_].type == "bs")
				{
					_loc1_.value = this.randomTaskAwardGem(this.selectTask.allReward[_loc3_].value);
				}
				else
				{
					_loc1_.value = this.selectTask.allReward[_loc3_].value;
				}
				_loc1_.cname = this.selectTask.allReward[_loc3_].cname;
				this.selectTask.rwaward.push(_loc1_);
			}
		}
		
		private function randomTaskAwardGem(param1:String):String
		{
			var _loc2_:* = null;
			var _loc3_:* = [];
			var _loc4_:int = 0;
			while (_loc4_ < param1.length)
			{
				_loc2_ = param1.substr(_loc4_, 1);
				_loc3_.push("wpqhs" + _loc2_);
				_loc3_.push("sms" + _loc2_);
				_loc3_.push("mfs" + _loc2_);
				_loc3_.push("gjs" + _loc2_);
				_loc3_.push("fys" + _loc2_);
				_loc4_++;
			}
			var _loc5_:uint = _loc3_.length;
			var _loc6_:uint = Math.round(Math.random() * (_loc5_ - 1));
			return _loc3_[_loc6_];
		}
		
		private function getroomhorsePet():PetDataVO
		{
			var _loc1_:PetDataVO = new PetDataVO();
			_loc1_.setPetNameAndLevel("roomhorse1", 1);
			return _loc1_;
		}
		
		override protected function onAdded():void
		{
			this.getaward.gotoAndStop(1);
			this.dailymc.addEventListener(MouseEvent.CLICK, this.dailyClick);
			this.activitymc.addEventListener(MouseEvent.CLICK, this.activityClick);
			this.prepage.addEventListener(MouseEvent.CLICK, this.prePage);
			this.nextpage.addEventListener(MouseEvent.CLICK, this.nextPage);
			this.dailymc.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
	
		override protected function onRemoved():void
		{
			this.dailymc.removeEventListener(MouseEvent.CLICK, this.dailyClick);
			this.activitymc.removeEventListener(MouseEvent.CLICK, this.activityClick);
			this.getaward.removeEventListener(MouseEvent.CLICK, this.awardClick);
			this.prepage.removeEventListener(MouseEvent.CLICK, this.prePage);
			this.nextpage.removeEventListener(MouseEvent.CLICK, this.nextPage);
			for (var idx:int = 0; idx < 5; idx++)
			{
				if (this["t" + (idx + 1)] == null)
				{
					break;
				}
				(this["t" + (idx + 1)] as TaskTitle).removeEventListener(MouseEvent.CLICK, this.selected);
			}
		}
		
		private function dailyClick(param1:MouseEvent):void
		{
			this.dailymc.gotoAndStop(2);
			this.activitymc.gotoAndStop(1);
			this.curAry = this.gc.allTask.getdayTask();
			this.setTaskList(this.curAry);
			var _loc2_:uint = this.curAry.length;
			this.curPage = 1;
			this.allPage = Math.ceil(_loc2_ / 5);
			this.setPageTxt();
		}
		
		private function activityClick(param1:MouseEvent):void
		{
			this.dailymc.gotoAndStop(1);
			this.activitymc.gotoAndStop(2);
			this.curAry = this.gc.allTask.getactTask();
			this.setTaskList(this.curAry);
			var _loc2_:uint = this.curAry.length;
			this.curPage = 1;
			this.allPage = Math.ceil(_loc2_ / 5) > 0 ? Math.ceil(_loc2_ / 5) : 1;
			this.setPageTxt();
		}
		
		private function awardClick(param1:MouseEvent):void
		{
			this.analyseAward();
		}
		
		private function prePage(param1:MouseEvent):void
		{
			if (this.curPage > 1)
			{
				--this.curPage;
			}
			else
			{
				this.curPage = 1;
			}
			this.setTaskListToFirstFrame();
			this.setTaskList(this.curAry);
			this.setPageTxt();
		}
		
		private function nextPage(param1:MouseEvent):void
		{
			if (this.curPage < this.allPage)
			{
				++this.curPage;
			}
			else
			{
				this.curPage = this.allPage;
			}
			this.setTaskListToFirstFrame();
			this.setTaskList(this.curAry);
			this.setPageTxt();
		}
		
		private function selected(param1:MouseEvent):void
		{
			this.getaward.removeEventListener(MouseEvent.CLICK, this.awardClick);
			this.setTaskListToFirstFrame();
			(param1.currentTarget as TaskTitle).selected();
			this.selectTask = (param1.currentTarget as TaskTitle).getTask();
			this.selectId = int(String(param1.currentTarget.name).substr(1, 1));
			this.setTxt();
			if (this.selectTask.getHasGetAward())
			{
				this.getaward.gotoAndStop(1);
			}
			else if (this.selectTask.judgeComplete())
			{
				this.getaward.gotoAndStop(2);
				this.getaward.addEventListener(MouseEvent.CLICK, this.awardClick);
			}
			else
			{
				this.getaward.gotoAndStop(1);
			}
			this.setAwardList();
		}
	}
}
